package com.ruoyi.web.controller.community.owner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.community.service.IOwnerActivityEnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.community.domain.OwnerActivityEnroll;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/community/owner/enroll")
public class OwnerActivityEnrollController extends BaseController {
    @Autowired
    private IOwnerActivityEnrollService ownerActivityEnrollService;

    /**
     * 查询活动报名列表
     */
//    @PreAuthorize("@ss.hasPermi('community:enroll:list')")
    @GetMapping("/list")
    public TableDataInfo listEnroll(OwnerActivityEnroll ownerActivityEnroll)
    {
        startPage();
        List<OwnerActivityEnroll> list = ownerActivityEnrollService.selectOwnerActivityEnrollList(ownerActivityEnroll);
        return getDataTable(list);
    }

    /**
     * 查询 活动报名记录 和 报名用户信息
     */
    @GetMapping("/list/{activityId}")
    public AjaxResult listEnrollById(@PathVariable("activityId") String activityId)
    {
//        List<SysUser> list = ownerActivityEnrollService.selectOwnerActivityEnrollListById(activityId);

        List<SysUser> userList = ownerActivityEnrollService.selectOwnerActivityEnrollListById(activityId);
        OwnerActivityEnroll query = new OwnerActivityEnroll();
        query.setActivityId(activityId);
        List<OwnerActivityEnroll> enrollList = ownerActivityEnrollService.selectOwnerActivityEnrollList(query);
        ArrayList<Object> list = new ArrayList<>();
        for (int i = 0; i < enrollList.size(); i++) {
            Map enrollDict = new HashMap<>();
            enrollDict.put("record", enrollList.get(i));
            enrollDict.put("userInfo", userList.get(i));
            list.add(enrollDict);
        }
        return AjaxResult.success(list);
    }


    /**
     * 导出活动报名详情列表
     */
//    @PreAuthorize("@ss.hasPermi('community:enroll:export')")
    @Log(title = "活动报名详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult exportEnroll(OwnerActivityEnroll ownerActivityEnroll)
    {
        List<OwnerActivityEnroll> list = ownerActivityEnrollService.selectOwnerActivityEnrollList(ownerActivityEnroll);
        ExcelUtil<OwnerActivityEnroll> util = new ExcelUtil<OwnerActivityEnroll>(OwnerActivityEnroll.class);
        return util.exportExcel(list, "活动报名详情数据");
    }

    /**
     * 报名
     */
//    @PreAuthorize("@ss.hasPermi('community:activityDetail:add')")
    @Log(title = "活动详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OwnerActivityEnroll ownerActivityEnroll)
    {
        return toAjax(ownerActivityEnrollService.insertOwnerActivityEnroll(ownerActivityEnroll));
    }
}
